#include <jni.h>
#include <stdio.h>
#include <cstdlib>
#include <stdio.h>
#include <windows.h> 
#include <iostream>
#include <fstream>
#include <string>
#include "assvapi.h"
#include "ssvapi.h"


JNIEXPORT jbyteArray JNICALL Java_visiodent_assvapi_ssvLireCartePS
  (JNIEnv *env, jobject, jstring NomRessourcePS, jstring NomRessourceLecteur, jstring CodePorteurPS, jstring pathDLL, jstring pathSesam)
{
    ssvapi* pSSVAPI = new ssvapi();
    
    // create native string to avoid crash the JVM.
    const char* nativeNomRessourcePS = (env)->GetStringUTFChars(NomRessourcePS, 0); 
    const char* nativeNomRessourceLecteur = (env)->GetStringUTFChars(NomRessourceLecteur, 0);
    const char* nativeCodePorteurPS = (env)->GetStringUTFChars(CodePorteurPS, 0);
    const char* pDLL = (env)->GetStringUTFChars(pathDLL, 0);
    const char* pSesam = (env)->GetStringUTFChars(pathSesam, 0);

    const char* date = "20130409"; // n'importe
    
    pSSVAPI->setData(nativeNomRessourcePS, nativeNomRessourceLecteur, nativeCodePorteurPS, date, pDLL, pSesam);
    
    string pBuffer = pSSVAPI->LireCartePS();
    const char* charBuffer = pBuffer.c_str();
    jsize len = pBuffer.length();
    jbyteArray output = env->NewByteArray(len);   
    env->SetByteArrayRegion(output, 0, len, (jbyte*)charBuffer);
   
    // release string 
    (env)->ReleaseStringUTFChars(NomRessourcePS, nativeNomRessourcePS);
    (env)->ReleaseStringUTFChars(NomRessourceLecteur, nativeNomRessourceLecteur);
    (env)->ReleaseStringUTFChars(CodePorteurPS, nativeCodePorteurPS);
    (env)->ReleaseStringUTFChars(pathDLL, pDLL);
    (env)->ReleaseStringUTFChars(pathSesam, pSesam);
    
    // free pointer
    free(pSSVAPI);
    
    return output;
}       

JNIEXPORT jbyteArray JNICALL Java_visiodent_assvapi_ssvLireDroitsVitale
  (JNIEnv *env, jobject, jstring NomRessourcePS, jstring NomRessourceLecteur, jstring CodePorteurPS, jstring date, jstring pathDLL, jstring pathSesam)
{
    ssvapi* pSSVAPI = new ssvapi();
    
    // create native string to avoid crash the JVM.
    const char* nativeNomRessourcePS = (env)->GetStringUTFChars(NomRessourcePS, 0); 
    const char* nativeNomRessourceLecteur = (env)->GetStringUTFChars(NomRessourceLecteur, 0);
    const char* nativeCodePorteurPS = (env)->GetStringUTFChars(CodePorteurPS, 0);
    const char* nativeDate = env->GetStringUTFChars(date, 0);
    const char* pDLL = (env)->GetStringUTFChars(pathDLL, 0);
    const char* pSesam = (env)->GetStringUTFChars(pathSesam, 0);

    
    pSSVAPI->setData(nativeNomRessourcePS, nativeNomRessourceLecteur, nativeCodePorteurPS, nativeDate, pDLL, pSesam);
    
    string pBuffer = pSSVAPI->LireDroitsVitale();
    const char* charBuffer = pBuffer.c_str();
    jsize len = pBuffer.length();
    jbyteArray output = env->NewByteArray(len);
    env->SetByteArrayRegion(output, 0, len, (jbyte*)charBuffer);
    
    // release string 
    (env)->ReleaseStringUTFChars(NomRessourcePS, nativeNomRessourcePS);
    (env)->ReleaseStringUTFChars(NomRessourceLecteur, nativeNomRessourceLecteur);
    (env)->ReleaseStringUTFChars(CodePorteurPS, nativeCodePorteurPS);
    (env)->ReleaseStringUTFChars(date, nativeDate);
    (env)->ReleaseStringUTFChars(pathDLL, pDLL);
    (env)->ReleaseStringUTFChars(pathSesam, pSesam);
    // free pointer
    free(pSSVAPI);
    
    return output;
}